package com.edushare.common.dao;

import java.util.ArrayList;
import java.util.List;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.GradingItem;

public class GradingItemDAO extends CayenneBasedDAO {

	public GradingItemDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}

	public void addGradingItem(CourseInstances cInst, String name, String desc,
			int gradeType, Double maxMarks, Double weightage, int itemType) {
		GradingItem gItem = new GradingItem();
		gItem.setCourseInstance(cInst);
		gItem.setGradeType(gradeType);
		gItem.setGradingItemDesc(desc);
		gItem.setGradingItemName(name);
		gItem.setMaxMarks(maxMarks);
		gItem.setWeightage(weightage);
		gItem.setItemType(itemType);

		getDataContext().registerNewObject(gItem);
		getDataContext().commitChanges();
	}

	public void addAssignmentGradingItem(Assignments assgn, int gradeType,
			Double maxMarks, Double weightage) {
		GradingItem gItem = new GradingItem();
		gItem.setAssignment(assgn);
		gItem.setCourseInstance(assgn.getCourseInstance());
		gItem.setGradeType(gradeType);
		gItem.setMaxMarks(maxMarks);
		gItem.setWeightage(weightage);
		gItem.setItemType(GradingItem.ITEM_COURSE_ASSGN);

		getDataContext().registerNewObject(gItem);
		getDataContext().commitChanges();
	}

	public List<GradingItem> getGradingItemsByCourse(CourseInstances cInst) {
		return cInst.getGradingItems();
	}

	public List<GradingItem> getGradingItemsByCourse(CourseInstances cInst,
			int itemType) {
		List<GradingItem> itList = cInst.getGradingItems();
		List<GradingItem> itTypeList = new ArrayList<GradingItem>();
		for (GradingItem it : itList) {
			if (it.getItemType() == itemType) {
				itTypeList.add(it);
			}
		}
		return itTypeList;
	}

	public void removeGradingItem(GradingItem item) {
		getDataContext().deleteObjects(item.getGrades());
		getDataContext().deleteObject(item);
		getDataContext().commitChanges();
	}
}
